<?php

namespace App\Trip\Plane\Interfaces\Result\Wrappers;

use App\Trip\Plane\Interfaces\Result\Abstracts\AbstractFlightQueryResult;
use App\Trip\Plane\Requests\TicketQueryRequest;
use Illuminate\Support\Collection;

class PlaneQueryResultSortWrapper extends AbstractFlightQueryResult
{
    private TicketQueryRequest        $request;
    private Collection                $lists;
    private AbstractFlightQueryResult $res;

    public function __construct(AbstractFlightQueryResult $res, TicketQueryRequest $request)
    {
        $this->res     = $res;
        $this->lists   = $res->getLists();
        $this->request = $request;
    }

    public function getLists(): Collection
    {
        $sort_key   = $this->request->getOrderBy();
        $sort_value = $this->request->getSortBy();
        if ($sort_value && in_array($sort_key, ['depart_time', 'flight_price'])) {
            $sb          = $sort_value == 1 ? 'sortBy' : 'sortByDesc';
            $this->lists = $this->lists
                ->$sb($sort_key);
        }
        // 去除键值
        return $this->lists;
    }

    public function getCount(): int
    {
        // 返回原始数据的总数量
        return $this->res->getCount();
    }
}